package com.fs.service.Impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.db.sql.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.mapper.HouseMapper;
import com.fs.pojo.House;
import com.fs.service.HouseService;
import com.fs.vo.Condition;
import com.fs.vo.ResponseData;
import com.fs.vo.ResultEnums;
import com.fs.vo.Search;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yyds
 * @fileName HouseServiceImpl
 * @date 2024/9/10 16:06
 * @description
 **/
@Service
public class HouseServiceImpl implements HouseService {
    @Resource
    private HouseMapper houseMapper;


    @Override
    public Page<House> findAllPage(Condition condition) {
        Page<House> page = new Page<>(condition.getPage(), condition.getCount());
        houseMapper.selectPage(page, null);
        return page;
    }

    @Override
    public House findById(Integer id) {
        return houseMapper.findById(id);
    }

    @Override
    public Boolean addHouse(House house) {
        return houseMapper.insert(house) > 0;
    }

    @Override
    public ResponseData<List<House>> myHouse(Integer id) {
        LambdaQueryWrapper<House> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(House::getPid, id);
        List<House> list = houseMapper.selectList(wrapper);
        return !list.isEmpty() ? new ResponseData<>(ResultEnums.OK, list) : new ResponseData<>(ResultEnums.NOT_FOUND);
    }

    @Override
    public Boolean deleteHouse(Integer id) {
        return houseMapper.deleteById(id) > 0;
    }

    @Override
    public Boolean updateHouse(House house) {
        return houseMapper.updateById(house) > 0;
    }

    @Override
    public Page<House> search(Search search) {
        Page<House> page = new Page<>(search.getPage(), search.getCount());
        LambdaQueryWrapper<House> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(search.getTitle()), House::getTitle, search.getTitle())
                .eq(search.getDid() > 0, House::getDid, search.getDid())
                .eq(search.getSid() > 0, House::getSid, search.getSid())
                .like(StrUtil.isNotBlank(search.getShi()), House::getRoom, search.getShi())
                .like(StrUtil.isNotBlank(search.getTing()), House::getRoom, search.getTing())
                .gt(search.getMin() > 0, House::getMoney, search.getMin())
                .le(search.getMax() > 0, House::getMoney, search.getMax())
                .gt(search.getTime() != null, House::getTime, search.getTime());
        for (Integer integer : search.getTid()) {
            wrapper.eq(House::getTid, integer).or();
        }
        houseMapper.selectPage(page,wrapper);
        return page;
    }
}
